<?php

// Init owncloud

require_once(OC_App::getAppPath('resumes') . "/lib/helper.php");

OCP\JSON::checkLoggedIn();
OCP\JSON::callCheck();

$app = PFactory::getApplication();
$resumedir = $app->getUserState('resumedir', null);
if($resumedir) {
   $neworderby = JRequest::getString('neworderby', '');
   $neworderdir = JRequest::getString('neworderdir', '');
   $orderby = JRequest::getString('orderby', '');
   $orderdir = strtoupper(JRequest::getString('orderdir', ''));
   $topnlist = JRequest::getInt('topnlist', 0); 

   if($neworderby == $orderby) {
      $orderdir = ($orderdir == 'ASC' ? 'DESC' : 'ASC'); 
   }
   else {
      $orderdir = strtoupper($neworderdir); 
      $orderdir == 'ASC' ? 'ASC' : 'DESC'; 
   }

   $resumedir['orderby'] = $neworderby;
   $resumedir['orderdir'] = $orderdir; 

   // if topnlist is true and neworderby is not matchresult, we need to setup porderby - priority order by 
   // for topnlist, the porderdir must be desc 
   if($topnlist && ($neworderby != 'matchresult' || $orderdir != 'DESC')) {
      $resumedir['porderby'] = 'matchresult';
      $resumedir['porderdir'] = "DESC";
   }
   else if(isset($resumedir['porderby'])) {
      unset($resumedir['porderby']);
      unset($resumedir['porderdir']);
   }

   $app->setUserState('resumedir', $resumedir);
}

OC_JSON::success(array("data" =>array()));
